package com.mf.controller.basic;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mf.aspect.debounces.Debounce;
import com.mf.aspect.logApi.LogApi;
import com.mf.entity.dto.basic.DtoPermission;
import com.mf.entity.req.basic.ReqPermission;
import com.mf.entity.req.basic.search.SearchReqPermission;
import com.mf.entity.vo.basic.voPublic.VoPublicKV;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.service.basic.ServicePermission;
import com.mf.utils.UtilConverter;
import com.mf.utils.tree.UtilTree;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限表
 */
@RestController
@RequestMapping("permission")
@SaCheckLogin
@Tag(name = "权限表")
@Debounce
public class ControllerPermission {
    @Resource
    private ServicePermission servicepermission;


    @Operation(summary = "列表树结构查询")
    @SaCheckPermission("permission:search")
    @PostMapping("/listTree")
    public Result<List<DtoPermission>> list(@RequestBody SearchReqPermission searchReqPermission) {
        return Result.success(UtilTree.filterTree(servicepermission.list(),
                servicepermission.findListByParams(searchReqPermission)));
    }

    @Operation(summary = "根据角色id查询权限")
    @SaCheckPermission({"permission:edit", "role:config"})
    @GetMapping("/findByRoleId")

    public Result<List<DtoPermission>> findByRoleId(@RequestParam("roleId") Integer roleId) {
        return Result.success(UtilTree.buildTree(servicepermission.findByRoleId(roleId)));
    }

    @Operation(summary = "根据角色id查询待绑定权限")

    @SaCheckPermission({"permission:edit", "role:config"})
    @GetMapping("/findByNoRoleId")
    public Result<List<DtoPermission>> findByNoRoleId(@RequestParam("roleId") Integer roleId) {
        return Result.success(UtilTree.buildTree(servicepermission.findByNoRoleId(roleId)));
    }

    @Operation(summary = "根据类型查询权限码")
    @SaCheckPermission("webMenu:edit")
    @GetMapping("findByType")
    public Result<List<VoPublicKV>> permissionByType(@RequestParam List<String> type) {
        return Result.success(servicepermission.findByType(type));
    }


    @Operation(summary = "保存")
    @SaCheckPermission("permission:edit")
    @PostMapping("/save")
    @LogApi("保存权限")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqPermission reqPermission) {
        servicepermission.save(UtilConverter.converter(reqPermission, DtoPermission.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission("permission:edit")
    @LogApi("修改权限")
    @PostMapping("/update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqPermission reqPermission) {
        servicepermission.permissionUpdateById(UtilConverter.converter(reqPermission, DtoPermission.class));
        return Result.success();
    }

    @Operation(summary = "复制权限码")
    @SaCheckPermission("permission:edit")
    @LogApi("复制权限码")
    @GetMapping("/copy")
    public Result<Void> copy(@RequestParam Integer id) {
        servicepermission.copy(id);
        return Result.success();
    }


    @Operation(summary = "删除")
    @SaCheckPermission("permission:delete")
    @LogApi("删除权限")
    @GetMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        servicepermission.deleteById(id);
        return Result.success();
    }
}
